package com.inforich.app.api.controller.company;

import cn.hutool.core.bean.BeanUtil;
import com.inforich.app.api.dto.TransportCompanyDTO;
import com.inforich.app.api.dto.TransportCompanyPermitDTO;
import com.inforich.app.api.service.IAppTransportCompanyService;
import com.inforich.app.api.vo.TransportCompanyVO;
import com.inforich.app.component.LoginUser;
import com.inforich.app.component.LoginUserApp;
import com.inforich.app.component.PreAppRole;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.company.domain.TransportCompany;
import com.inforich.company.service.ITransportCompanyService;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 运输公司
 * @author: HJM
 * @date: 2022/10/29
 */
@RestController
@RequestMapping("/h5api/transportCompany")
public class AppTransportCompanyController {

  @Autowired
  private ITransportCompanyService transportCompanyService;
  @Autowired
  private IAppTransportCompanyService appTransportCompanyService;

  /**
   * 运输企业列表
   */
  @GetMapping("/all")
  public AjaxResult all() {
    Map<String, Object> map = new HashMap<>();
    map.put("valid", "1");
    return AjaxResult.success(transportCompanyService.listTransportCompany(map));
  }

  /**
   * 添加公司信息
   */
  @PostMapping("/add")
  @PreAppRole("transport_company_admin")
  public AjaxResult addCompany(@LoginUser LoginUserApp loginUserApp,
      @Validated @RequestBody TransportCompanyDTO dto) {

    return appTransportCompanyService.addCompany(loginUserApp, dto);
  }

  /**
   * 我的项目
   */
  @GetMapping("/myCompany")
  @PreAppRole("transport_company_admin")
  public AjaxResult myCompany(@LoginUser LoginUserApp loginUserApp) {
    TransportCompany company = appTransportCompanyService.getMyCompany(loginUserApp);
    if (company == null) {
      return AjaxResult.error("暂无公司信息");
    }
    TransportCompanyVO vo = new TransportCompanyVO();
    BeanUtil.copyProperties(company, vo);
    return AjaxResult.success(vo);
  }

  /**
   * 更新准运证信息
   */
  @PostMapping("/updateTransportPermitInfo")
  @PreAppRole("transport_company_admin")
  public AjaxResult updateTransportPermitInfo(@LoginUser LoginUserApp loginUserApp,
      @Validated @RequestBody TransportCompanyPermitDTO dto) {
    return appTransportCompanyService.updateTransportPermitInfo(loginUserApp, dto);
  }

}
